package com.wn.search.viewmodel

import com.wn.common.model.ArticleList
import com.wn.common.model.HotSearch
import com.wn.network.viewmodel.BaseViewModel
import com.wn.search.repository.SearchRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2024/12/13 17:19
 * @Description: 搜索 ViewModel
 */
@HiltViewModel
class SearchViewModel @Inject constructor(private val repository: SearchRepository) :
  BaseViewModel() {

  /**
   * 搜索热词
   * @return StateFlow<MutableList<HotSearch>?>
   */
  fun getHotSearchData(): StateFlow<MutableList<HotSearch>?> =
    callBaseRequest({ scope, errorCall, showLoading ->
      repository.getHotSearchData(
        scope,
        errorCall,
        showLoading
      )
    })

  /**
   * 搜索结果
   * @param page Int                      --  页码
   * @param keyWord String                --  关键词，支持多个，空格分开
   * @return StateFlow<ArticleList?>
   */
  fun searchResult(page: Int, keyWord: String): StateFlow<ArticleList?> =
    callBaseRequest({ scope, errorCall, showLoading ->
      repository.searchResult(
        scope,
        errorCall,
        showLoading,
        page,
        keyWord
      )
    }, true)

  /**
   * 收藏 or 取消收藏站内文章
   * @param id Int                  -- 文章 id
   * @param isCollect Boolean       -- 是否收藏
   * @return StateFlow<Any?>
   */
  fun collectArticle(id: Int, isCollect: Boolean): StateFlow<Any?> =
    callBaseRequest({ scope, errorCall, showLoading ->
      repository.collectArticle(
        scope,
        errorCall,
        showLoading,
        id,
        isCollect
      )
    }, true)
}